package com.fruit.web.vo.customer;

import java.io.IOException;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import net.sf.cglib.beans.BeanCopier;

import com.fruit.service.IOrderService;
import com.fruit.utils.BeanCopyUtil;
import com.fruit.utils.DESPlusUtil;
import com.fruit.utils.DeliveryUtil;
import com.fruit.utils.MessageFactory;
import com.fruit.utils.PaymentTypeUtil;
import com.fruit.utils.PaymentUtil;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.web.bo.impl.Consignee;
import com.fruit.web.bo.impl.Customer;
import com.fruit.web.bo.impl.Order;
import com.fruit.web.bo.impl.OrderDetail;
import com.fruit.web.bo.impl.Productunit;
import com.fruit.web.constant.EjdConstants;
import com.fruit.web.constant.ManageBeanConstants;
import com.fruit.web.constant.SpringBeanConstants;
import com.fruit.web.vo.common.ConsigneeVo;
import com.fruit.web.vo.common.CustomerVo;
import com.fruit.web.vo.common.OrderCrudBean;
import com.fruit.web.vo.common.OrderDetailVo;
import com.fruit.web.vo.common.OrderVo;
import com.fruit.web.vo.common.ProductunitVo;

@ManagedBean(name="customerUpdateOrderPage")
@ViewScoped
public class CustomerUpdateOrderPageBean extends OrderCrudBean implements java.io.Serializable {
	private String globalErrorMessage;
	
    private boolean couponUsed;
    
	private boolean integrationUsed;
	
	public String getGlobalErrorMessage() {
		return globalErrorMessage;
	}

	public void setGlobalErrorMessage(String globalErrorMessage) {
		this.globalErrorMessage = globalErrorMessage;
	}

	public boolean isCouponUsed() {
		return couponUsed;
	}

	public void setCouponUsed(boolean couponUsed) {
		this.couponUsed = couponUsed;
	}

	public boolean isIntegrationUsed() {
		return integrationUsed;
	}

	public void setIntegrationUsed(boolean integrationUsed) {
		this.integrationUsed = integrationUsed;
	}

	public CustomerUpdateOrderPageBean() throws IOException,Exception {
		init();
		//following code is same as ShopCartBean start
		this.setCouponChecked(false);
		this.setCouponScore(0D);
		this.setIntegrationChecked(false);
		this.setIntegrationScore(0D);
		//set delivery
		this.setDeliveries(DeliveryUtil.getDeliveries());
		//set payment
		this.setPayments(PaymentUtil.getPayments());
		//set paymenttype
		this.setPaymentTypes(PaymentTypeUtil.getPaymentTypes());
		//set default address
		this.setActionSetAddress(EjdConstants.ACTION_VIEW_TYPE);
		//set default invoice
		this.setActionSetInvoice(EjdConstants.ACTION_VIEW_TYPE);
		SelectItem[] tempAddressItems = new SelectItem[1];
		String firstDefaultAddressN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"select.one.address.message", FacesMessage.SEVERITY_ERROR).getDetail();
		tempAddressItems[0]= new SelectItem(EjdConstants.BLANK_VALUE_SELECT_ITEM,firstDefaultAddressN18);
		this.setAddressItems(tempAddressItems);
		this.setMaxBuyGoodsNumber(new Integer(EjdConstants.MAX_BUY_GOODS_NUMBER));
		//following code is same as ShopCartBean end
		//following code is search order
		//set default couponUsed and IntegrationUsed
		this.setCouponUsed(false);
		this.setIntegrationUsed(false);
		String orderId =(String) SpringFacesUtil.getRequestParameter("orderId");
		if (null == orderId || "".equals(orderId) || "null".equals(orderId)) {
			SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
			
		}else {
			DESPlusUtil desUtil = new DESPlusUtil();
			orderId = desUtil.decrypt(orderId);
			IOrderService<Order,Long> orderService = (IOrderService<Order,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.ORDER_SERVICE_BEAN_NAME);
			orderService.setEntityClass(Order.class);
			orderService.setPKClass(Long.class);
			Order theOrder = orderService.findUniqueBy("orderId", orderId);
			if (null != theOrder) {
				LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
				Customer orderOfCustomer = theOrder.getCustomer();
				if (!(loginCustomerPage.getCustomerInfo().getCustomerId().equals(orderOfCustomer.getCustomerId()))) {
					String authorizN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"order.view.not.authorized", FacesMessage.SEVERITY_ERROR).getDetail();
					this.setGlobalErrorMessage(authorizN18);
					this.setOrder(new OrderVo());
				} else {
					OrderVo theOrderVo = new OrderVo();
					BeanCopyUtil.OrderCopyToOrderVo(theOrder, theOrderVo);
					//copy customer
					BeanCopier copyCustomer = BeanCopier.create(Customer.class, CustomerVo.class, false);
					copyCustomer.copy(theOrder.getCustomer(), theOrderVo.getCustomer(), null);
					//copy order detail start
					if (null != theOrder.getOrderDetails() &&  !(theOrder.getOrderDetails().isEmpty())) {
						Set<OrderDetail> orderDetails = theOrder.getOrderDetails();
						Set<OrderDetailVo> orderDetailsVo = new HashSet<OrderDetailVo>();
						Iterator<OrderDetail> it = orderDetails.iterator();
						while (it.hasNext()) {
							OrderDetail tempOrderDetail= new OrderDetail();
							tempOrderDetail = it.next();
							OrderDetailVo tempOrderDetailVo = new OrderDetailVo();
							BeanCopier copyOrderDetail = BeanCopier.create(OrderDetail.class, OrderDetailVo.class, false);
							copyOrderDetail.copy(tempOrderDetail, tempOrderDetailVo, null);
							orderDetailsVo.add(tempOrderDetailVo);
						}
						theOrderVo.setOrderDetails(orderDetailsVo);
					}
					// copy order detail end
					if (null != this.getCouponScore() && this.getCouponScore() > 0) {
						this.setCouponUsed(true);
					}
					if (null != this.getIntegrationScore() && this.getIntegrationScore() > 0) {
						this.setIntegrationUsed(true);
					}
					this.setOrder(theOrderVo);
				}
			} else {
				SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
			}
		}
	}
	
}
